package com.laxcen.controls;


import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.laxcen.toydemo.R;

import util.RandomUtil;
import util.SoundClass;


/**
 * Created by Ningtab on 2017/2/22.
 */
public class VideoItemControl extends LinearLayout {

    private Context mContext;
    private ImageView img_video_poster;
    private Button img_video_number;

    private RelativeLayout layoutRoot;
    private OnClickListener onClickListener;

    public VideoItemControl(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        // TODO Auto-generated constructor stub
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.ctl_video_item, this);

        img_video_poster = (ImageView) findViewById(R.id.img_video_poster);
        img_video_number = (Button) findViewById(R.id.img_video_number);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.VideoItemControl);
        img_video_poster.setImageDrawable(a.getDrawable(R.styleable.VideoItemControl_videoImgSrc));
        img_video_number.setText(a.getText(R.styleable.VideoItemControl_videoNumberText));
        layoutRoot = (RelativeLayout) findViewById(R.id.listButton_Root);
        a.recycle();

        layoutRoot.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startOnClickAnimation();
                SoundClass soundClass = new SoundClass(mContext);
                soundClass.playSound(R.raw.play);

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (onClickListener != null)
                            onClickListener.onClick(VideoItemControl.this);
                    }
                }, 500);
            }
        });

        startOnLoadAnimation();
        startAnimationTimer();
    }

    private void startOnLoadAnimation() {
        Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.video_item_anim_splash);
        layoutRoot.startAnimation(animation);
    }

    private void startAnimationTimer() {
        int firstDelayMillis = RandomUtil.generateOneNumber(2000, 5000);
        final Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.video_item_anim_splash);
        Handler timerHandler = new Handler();
        Runnable timerRunnable = new Runnable() {
            @Override
            public void run() {
                layoutRoot.startAnimation(animation);
                int delayMillis = RandomUtil.generateOneNumber(2000, 10000);
                timerHandler.postDelayed(this, delayMillis);
            }
        };
        timerHandler.postDelayed(timerRunnable, firstDelayMillis);
    }

    private void startOnClickAnimation() {
        final Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.home_menu_button_anim_click);
        layoutRoot.startAnimation(animation);
    }

    public void setFakeBoldText(TextView tv) {
        TextPaint tp = tv.getPaint();
        tp.setFakeBoldText(true);
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        onClickListener = l;
        super.setOnClickListener(l);
    }


    @Override
    public void setEnabled(boolean enabled) {
//        super.setEnabled(enabled);
        layoutRoot.setEnabled(enabled);
//        if (enabled) {
//            listButton_Root.setClickable(true);
//        } else {
//            listButton_Root.setClickable(false);
//        }
    }

    /*
     * 摇晃，晃动*/
    public void shake() {
        final Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.home_menu_button_anim_click);
        layoutRoot.startAnimation(animation);
        SoundClass soundClass = new SoundClass(mContext);
        soundClass.playSound(R.raw.play);
    }


}
